<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Logic;

/**
 * 功能菜单模型业务逻辑
 *
 * @author derek <693472824@qq.com>
 */
class MenuLogic extends BaseLogic
{

    /**
     * 根据用户ID 获取指定字段的菜单数组
     *
     * @param integer $uid
     *            用户ID
     * @return array $uid拥有权限的菜单数组
     * @author derek <693472824@qq.com>
     */
    public function getMenusByUID ($uid = null, $menu_fields = 'menu_id')
    {
        $uid = empty($uid) ? AdminLogic::is_login() : $uid;
        $menus = session('ADMIN_MENU' . $uid);
        
        if (empty($menus)) {
            $admin_where['admin_id'] = $uid;
            $role_id = M('Admin')->where($admin_where)->getField('role_id');
            
            $menus = $this->getMenuByRID($role_id, $menu_fields);
            
            session('ADMIN_MENU' . $uid, $menus);
        }
        return $menus;
    }

    /**
     * 根据角色ID 获取指定字段的菜单数组
     *
     * @param integer $role_id
     *            角色ID
     * @return array $role_id 拥有权限的菜单数组
     * @author derek <693472824@qq.com>
     */
    public function getMenuByRID ($role_id = null, $menu_fields = 'menu_id') {
        
        $menu_array = array();
        if (empty($role_id)) {
            return $menu_array;
        }
        
        $menuid_str = M('Role')->where(array('role_id'=>$role_id))->getField('menu_id_str');
        $menu_id_array = empty($menuid_str) ? array() : explode(',', trim($menuid_str, ','));
        $menu_id_array = array_unique($menu_id_array);
        
        $map['menu_id'] = empty($menu_id_array) ? '' : array('in', $menu_id_array);
        
        $menu_array = $this->getMenus($map, $menu_fields);
        
        return $menu_array;
    }

    /**
     * 根据指定的查询条件，获取包含指定列名的菜单数组
     *
     * @param
     *            array 查询条件数组
     * @param
     *            string|boolean 指定列名|全部列名
     * @return array 菜单数组
     * @author derek <693472824@qq.com>
     */
    public function getMenus ($map = array(), $field = 'menu_id')
    {
        $map['show_status'] = '1';
        return M('Menu')->where($map)->order('parent_id, sort_no ASC')->getField($field);
    }

    /**
     * 保存 菜单信息以及菜单规则表信息的保存
     *
     * @param int|string $menuID
     *            菜单的id
     * @author derek <693472824@qq.com>
     */
    public function addMenu ()
    {
        $menuModel = D('Menu');
        $menuID = I('menu_id');
        $menu = $menuModel->create();
        if ($menu) {
            if (empty($menuID)) {
                
                return $menuModel->add();
            } else {
                
                return $menuModel->where('menu_id=' . $menuID)->save();
            }
        } else {
            $this->error = $menuModel->getError();
            return false;
        }
    }

    /**
     * 菜单删除以及菜单规则表的删除
     *
     * @param int|string $menu_id
     *            菜单ID
     *            
     *            
     * @return boolean true:代表成功 flase:代表失败
     * @author derek <693472824@qq.com>
     */
    public function del ()
    {
        $menu_id = I('menu_id');
        $menuModel = M('Menu');
        $menu_list = $menuModel->where('parent_id=' . $menu_id)->getField(
                'menu_id');
        if (empty($menu_list)) {
            $menu_where['menu_id'] = $menu_id;
        } else {
            $menu_where['menu_id'] = array(
                    'in',
                    $menu_list . ',' . $menu_id
            );
        }
        $result = $menuModel->where($menu_where)->delete();
        return $result;
    }

    /**
     * 根据菜单ID获取单个菜单
     *
     * @param int $menu_id
     *            菜单ID
     *            
     * @return object $menuModel 返回用户的一个模型
     * @author derek <693472824@qq.com>
     */
    public function getMenuByID ($menuID)
    {
        if (empty($menuID)) {
            return null;
        }
        $menu = D('Menu');
        $menu_where['menu_id'] = $menuID;
        $menuModel = $menu->where($menu_where)->find();
        return $menuModel;
    }

    /**
     * 获取 菜单树
     *
     * @param $parent_id :父级节点            
     * @author derek <693472824@qq.com>
     * @return 返回部门的树形结构
     *
     */
    public function getMenuTree ($parent_id = '0')
    {
        $menu_tree = array();
        
        if (! is_numeric($parent_id)) {
            return $menu_tree;
        }
        $menuModel = D('Menu');
        $menulist = $menuModel->select();
        
        $menu_tree = api('Util/list_to_tree', 
                array(
                        $menulist,
                        'menu_id',
                        'parent_id',
                        '_child',
                        $parent_id
                ));
        
        return $menu_tree;
    }
}

?>